/**
 * 
 */
package com.xb.cloud.product.merchant.service.impl;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xb.cloud.common.constant.DbConstants;
import com.xb.cloud.common.util.BeanUtils;
import com.xb.cloud.product.merchant.service.client.EndMerchantUserService;
import com.xb.cloud.product.merchant.service.client.bo.EndMerchantUserBo;
import com.xb.cloud.product.service.business.model.AdminUser;
import com.xb.cloud.product.service.business.model.AdminUserExample;
import com.xb.cloud.product.service.business.model.AdminUserExample.Criteria;
import com.xb.cloud.product.service.business.model.mapper.AdminUserMapper;
import com.xb.cloud.service.client.ServiceReturnCodes;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;

/**
 * 前端商家用户服务类
 * 
 * @author kexue
 *
 */
@Service
public class EndMerchantUserServiceImpl extends AbstractService implements EndMerchantUserService {

	@Resource
	private AdminUserMapper adminUserMapper;

	/**
	 * 
	 * @param id
	 *            : 商家id
	 */
	@Override
	public ServiceReturnInfo getEndMerchantUserById(Long id) {

		AdminUserExample adminUserExample = new AdminUserExample();
		Criteria createCriteria = adminUserExample.createCriteria();
		createCriteria.andIsDeleteEqualTo(DbConstants.IS_DELETED_FALSE);
		createCriteria.andAccountTypeIn(
				Arrays.asList(new Byte[] { DbConstants.ACCOUNT_TYPE_LEAF_MERCHANT, DbConstants.ACCOUNT_TYPE_SHOP }));
		createCriteria.andIdEqualTo(id);

		List<AdminUser> adminUserList = adminUserMapper.selectByExample(adminUserExample);
		if (adminUserList.isEmpty()) {
			return ServiceReturnUtils.success(null);
		} else {
			EndMerchantUserBo endMerchantUserBo = new EndMerchantUserBo();
			AdminUser data = adminUserList.get(0);
			BeanUtils.copyProperties(data, endMerchantUserBo);
			return ServiceReturnUtils.success(endMerchantUserBo);
		}
	}

	@Override
	public ServiceReturnInfo getEndMerchantUserByPhoneNum(Long companyId, String phoneNum) {
		
		AdminUserExample adminUserExample = new AdminUserExample();
		Criteria createCriteria = adminUserExample.createCriteria();
		createCriteria.andPhoneNumEqualTo(phoneNum).andCompanyIdEqualTo(companyId).andAccountTypeGreaterThan((byte)10);
		List<AdminUser> adminUsers=adminUserMapper.selectByExample(adminUserExample);
		if(adminUsers.isEmpty()) {
			return ServiceReturnUtils.success(null);
		}else {
			EndMerchantUserBo endMerchantUserBo = new EndMerchantUserBo();
			BeanUtils.copyProperties(adminUsers.get(0), endMerchantUserBo);
			return ServiceReturnUtils.success(endMerchantUserBo);
		}
		
	}

	@Override
	public ServiceReturnInfo updateByPrimaryKey(EndMerchantUserBo endMerchantUserBo) {
		AdminUser adminUser = new AdminUser();
		BeanUtils.copyProperties(endMerchantUserBo, adminUser);
		adminUser.setModifiedTime(timeService.getDate());
		adminUserMapper.updateByPrimaryKey(adminUser);
		return ServiceReturnUtils.success();
		
	}

	@Override
	public ServiceReturnInfo updatePassword(EndMerchantUserBo merchantUserBo) {
		AdminUser adminUser = new AdminUser();
		BeanUtils.copyProperties(merchantUserBo, adminUser);
		adminUser.setModifiedTime(timeService.getDate());
		int updated=adminUserMapper.updateByPrimaryKeySelective(adminUser);
		
		return ServiceReturnUtils.success(updated);
	}

	@Override
	public ServiceReturnInfo createEndMerchantUser(EndMerchantUserBo merchantUserBo) {
		if (merchantUserBo == null) {
			return ServiceReturnUtils.error(ServiceReturnCodes.PARAM_IS_NULL);
		}
		//主键自增     merchantUserBo.setId(ipKeyGenerator.generateKey());
		AdminUser adminUser = new AdminUser();
		BeanUtils.copyProperties(merchantUserBo, adminUser);
		adminUser.setCreateTime(timeService.getDate());
		//设置默认loginName
		adminUser.setLoginName(merchantUserBo.getPhoneNum());
		adminUserMapper.insertSelective(adminUser);
		return ServiceReturnUtils.success();
	}

}
